<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="新建CID用户" :visible.sync="dialogVisible" @close="resetForm('addForm')" width="400px">
        <el-form label-position="top" :model="addForm" :hide-required-asterisk='true' ref="addForm" :rules="addPddRules" size="medium">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="addForm.username" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item label="昵称" prop="nickname">
                <el-input v-model="addForm.nickname" placeholder="昵称"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="addForm.password" placeholder="密码" show-password></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="rel_password">
                <el-input v-model="addForm.rel_password" placeholder="确认密码" show-password></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="resetForm('addForm')" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm('addForm')" :disabled="loading" :loading="loading" class="el-button-width">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            loading: false,
            dialogVisible: false,
            userList: [],
            addForm: {
                username: "",
                nickname: "",
                password:'',
                rel_password:'',
            },
            addPddRules: {
                username: [
                    {
                        required: true,
                        message: "请输入用户名",
                        trigger: ["blur", "change"],
                    },
                ],
                nickname: [
                    {
                        required: true,
                        message: "请输入昵称",
                        trigger: ["blur", "change"],
                    },
                ],
                password: [
                    {
                        required: true,
                        message: "请输入密码",
                        trigger: ["blur", "change"],
                    },
                ],
                rel_password: [
                    {
                        required: true,
                        message: "请输入确认密码",
                        trigger: ["blur", "change"],
                    },
                ],
            },
        };
    },
    created() {},
    methods: {
        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true;
                    const { data: res } = await this.$http.post(
                        "cid/User/userAdd",
                        this.addForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "添加成功",
                            type: "success",
                        });
                        this.loading = false;
                        this.$emit("getDataList");
                        this.resetForm(formName);
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },
    },
    components: {},
};
</script>


<style scoped>
.el-form-item {
    margin-bottom: 16px;
}
</style>
